/** @format */

const { Controller } = require("egg");

class SchoolController extends Controller {
    async index() {
        const { ctx } = this;
        ctx.body = "hi, egg";
    }
    async addschool() {
        const { ctx } = this;
        let clientdata = ctx.request.body;
        console.log(clientdata, "前端传参");
        let school_image = JSON.stringify(clientdata.school_image);
        const post = await this.app.mysql.insert("school", {
            school_name: clientdata.school_name,
            school_image: school_image,
            create_id: clientdata.create_id,
            school_address: clientdata.school_address,
        });
        if (post) {
            ctx.body = {
                code: 200,
                desp: "添加成功",
                post,
            };
        } else {
            ctx.body = {
                code: 201,
                desp: "添加失败",
            };
        }
    }
    async getschoollist() {
        const { ctx } = this;
        let clientdata = ctx.request.body;
        let data = {
            // 搜索 posts 表
            where: { ...(clientdata.Query ? clientdata.Query : "") }, // WHERE 条件
            limit: clientdata.pagesize, // 返回数据量
            offset: (clientdata.pageindex - 1) * clientdata.pagesize, // 数据偏移量
            orders: [["id"]], // 排序方式
        };
        console.log(data, "data");
        let schoollist = await this.app.mysql.select("school", data);
        await Promise.all(
            schoollist.map(async (item, index) => {
                let createinfo = await this.app.mysql.get("userinfo", { id: item.create_id });
                schoollist[index].createinfo = createinfo;
                let cruitinfo = await this.app.mysql.select("recruit", { where: { school_id: item.id } });
                schoollist[index].cruitinfo = cruitinfo;
            }),
        );
        if (schoollist) {
            ctx.body = {
                code: 200,
                desp: "查询成功",
                schoollist,
            };
        }
    }
    async addrecruit() {
        const { ctx } = this;
        let clientdata = ctx.request.body;
        console.log(clientdata, "前端传参");
        const post = await this.app.mysql.insert("recruit", {
            school_id: clientdata.id,
            detail: clientdata.detail,
            school_phone: clientdata.school_phone,
            title: clientdata.title,
        });
        if (post) {
            ctx.body = {
                code: 200,
                desp: "添加成功",
                post,
            };
        } else {
            ctx.body = {
                code: 201,
                desp: "添加失败",
            };
        }
    }
}

module.exports = SchoolController;
